package com.atguigu.gulimall.ware.listener;

import com.atguigu.common.to.mq.StockLockTo;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.atguigu.common.to.mq.OrderTo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

@Service
@RabbitListener(queues = "stock.release.stock.queue")
public class StockReleaseListener {
    @Resource
    WareSkuService wareSkuService;

    @RabbitHandler
    public void handleStockLockedRelease(StockLockTo to, Message message,Channel channel) throws IOException {
        try {
            wareSkuService.handleStockLockedRelease(to,message,channel);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

    @RabbitHandler
    public void handleOrderCloseRelease(OrderTo to, Message message,Channel channel) throws IOException {
        try {
            System.err.println("订单关闭，准备解锁库存");
            wareSkuService.handleOrderCloseRelease(to,message,channel);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
